package com.lzb.web.controller;

import java.math.BigDecimal;
import java.util.List;
import java.util.Random;
import java.util.UUID;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lzb.cache.MngMomentQueueCached;
import com.lzb.common.util.time.TimeUtil;
import com.lzb.domain.MngMoment;
import com.lzb.model.MngUserModel;
import com.lzb.service.FavouriteService;
import com.lzb.service.MngMomentService;
import com.lzb.service.MngUserService;

@RestController
@RequestMapping(value = "/rest/")
public class RestfulController extends BaseController {
	
	@Resource(name = MngMomentService.BEAN_PACKAGE_NAME)
	private MngMomentService mngMomentService;
	
	@Resource(name = MngUserService.BEAN_PACKAGE_NAME)
	private MngUserService mngUserService;
	
	@Resource(name = FavouriteService.BEAN_PACKAGE_NAME)
	private FavouriteService favouriteService;
	
	@Autowired
	private MngMomentQueueCached queue;
	
	private List<MngUserModel> userModels = null;
	
	private static Random random = new Random();
	
	@PostConstruct
	public void postConstruct() {
		userModels = mngUserService.findAll();
	}
	
	
	@RequestMapping(value = "doNotNeedSecuritySendMoment", method = RequestMethod.POST)
	public String doNotNeedSecuritySendMoment(String content) {
		MngMoment moment = new MngMoment();
		String uuid = UUID.randomUUID().toString().replaceAll("-", "");
		moment.setContent(content + ":" + Thread.currentThread().getName());
		moment.setUuid(uuid);
		String createDate = TimeUtil.getCurDate("yyyyMMdd");
		String createTime = TimeUtil.getCurDate("HHmmss");
		moment.setCreateDate(createDate);
		moment.setCreateTime(createTime);
		MngUserModel user = userModels.get(random.nextInt(userModels.size()));
		String userUUID = user.getUuid();
		try {
			queue.lpush(moment);
			favouriteService.doAddUserMoment(userUUID, uuid, new BigDecimal(createDate + createTime).doubleValue());
		} catch (Exception e) {
			e.printStackTrace();
			return "添加失败";
		}
		return "添加成功";
	}
}
